package ch.idsia.ai.agents.controllers.myai.tests;

import ch.idsia.ai.agents.controllers.myai.*;
import org.junit.*;

import java.util.Vector;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

/**
 * Created by IntelliJ IDEA.
 * User: Blake
 * Date: Jul 1, 2010
 * Time: 8:24:15 PM
 * To change this template use File | Settings | File Templates.
 */
public class DecisionTreeTest
{
    @Test public void testDecisionTree()
    {
        DecisionTree test = new DecisionTree();
        assertNotNull(test);
    }

    @Test   public void testDecide()
    {
        DecisionTree test = new DecisionTree();
        Ratio[][] brain = new Ratio[6][3];
        for(int i = 0; i < 6; ++i)
            for(int j = 0; j < 3; ++j)
                brain[i][j] = new Ratio(1,1);
        
        Vector environment = new Vector();
        double learningRate = 1;
        Node decision;
        environment.clear();

        //Test the no Element in surroundings decisions.
        for (int i = 0; i < 100; ++i)
        {
            decision = test.decide(brain, learningRate, environment);
            assertTrue("testDecide: no Elements in environment.", decision.getName().equals("doRight") || decision.getName().equals("doRun"));
        }


        //Test the Enemy in surroundings decision.
        environment.clear();
        environment.add(new Enemy(1,0,2,1, (int)(Math.random() * 100)) );
        for(int i = 0; i < 1000; ++i)
        {
            decision = test.decide(brain, learningRate, environment);
            String data = decision.getName();
            assertTrue("testDecide: Enemy in environment.", data.equals("doRun") || data.equals("doJump") || data.equals("doShoot"));
        }

        //Test the obstacle in surroundings decision.
        environment.clear();
        environment.add(new Obstacle(0,0,1,1));
        for (int i = 0; i < 100; ++i)
        {
            decision = test.decide(brain, learningRate, environment);
            assertTrue("testDecide: Obstacle in environment.", decision.getName().equals("doJump"));
        }

    }

    @Test   public void testChangeAbleToShoot()
    {
        DecisionTree test = new DecisionTree();
        Ratio[][] brain = new Ratio[6][3];
        for(int i = 0; i < 6; ++i)
            for(int j = 0; j < 3; ++j)
                brain[i][j] = new Ratio(1,1);

        Vector environment = new Vector();
        double learningRate = 1;
        Node decision = null;
        environment.clear();

        test.changeAbleToShoot(false);
        environment.add(new Enemy(0, 0, 1, 1, 5));
        for (int i = 0; i < 1000; ++i)
        {
            decision = test.decide(brain, learningRate, environment);
            assertTrue("testChangeAbleToShoot", !decision.getName().equals("doShoot"));
        }

        test.changeAbleToShoot(true);
        for (int i = 0; i < 1000; ++i)
        {
            decision = test.decide(brain, learningRate, environment);
            if(decision.getName().equals("doShoot"))
                break;
        }
        assertTrue(decision.getName().equals("doShoot"));
    }
}
